$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/record/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 80 },
			{ label: 'uid', name: 'userId', index: 'userId', width: 80 },
			{ label: '手机账号', name: 'mobile', index: 'mobile', width: 120 },
			{ label: '类型', name: 'type', index: 'type', width: 80 , formatter: function(value, options, row){
					if(!value && value!=0){
						return '未知'
					}
					var g = {
						1 :"充值", 2: "提现", 3: "余额划转", 4: "下单支出",  5: "静态奖励", 6: "推荐奖励", 7: "等级分红", 8: "业绩分红", 9: "季度分红",23:"提现余额CNY",24:"提现余额USDT"
					}
					if(!g[value]){
						return '未知'
					}
					return g[value];
				}
			},
			{ label: '备注', name: 'remark', index: 'remark', width: 200 },
			{ label: '金额', name: 'amount', index: 'amount', width: 80 },
			{ label: '手续费', name: 'fee', index: 'fee', width: 80 },
			/*{ label: '哈希', name: 'hash', index: 'hash', width: 150 },
			{ label: '充值提现类型', name: 'operateType', index: 'operate_type', width: 120 , formatter: function(value, options, row){
					if(!value && value!=0){
						return ''
					}
					var g = {
						1 :"人民币充值/提现", 2 :"BSC 充值/提现", 3 :"Tron 充值/提现",
					}
					return g[value];
				}
			},
			{ label: '数字货币提现地址', name: 'address', index: 'address', width: 150 },
			{ label: '人民币提现银行卡', name: 'bankName', index: 'bank_name', width: 150 },
			{ label: '人民币提现银行卡号', name: 'bankNum', index: 'bank_num', width: 150 },
			{ label: '人民币提现人姓名', name: 'realName', index: 'real_name', width: 150 },*/
			{ label: '状态', name: 'status', index: 'status', width: 60 , formatter: function(value, options, row){
				if(!value && value!=0){
					return '未知'
				}
				var g = {
						0 :"未结算",  1: "已结算" ,2: "待确认"
					}
					if(!g[value]){
						return '未知'
					}
					return g[value];
				}
			},

			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 150 },
		],
		viewrecords: true,
        height: 480,
		scrollOffset:0,
        width: 1000,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
		autowidth:true,
		multiselect: true,
		shrinkToFit:false,
		autoScroll: true,
		pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
		postData: {
			"record": "flow",
		},
		beforeProcessing: function(data, status, xhr) {
			// 假设我们检查名为"errorField"的字段
			if (data.code !== 0) {
				// 如果满足条件，则显示错误消息
				alert(data.msg);
				// 停止处理，不显示数据
				return false;
			}
			// 如果没有错误，继续处理数据
			return true;
		},
        gridComplete:function(){
        	//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
	$("#createTime").datepicker({
		controlType: 'select',
		showOtherMonths: true,
		selectOtherMonths: true,
		maxDate: new Date()
	});
	$("#endTime").datepicker({
		controlType: 'select',
		showOtherMonths: true,
		selectOtherMonths: true,
		maxDate: new Date(),
	});
});

function showImg(val,tit){
	$("#showImg").attr("src",val);
	layer.open({
		type: 1,
		skin: 'layui-layer-molv',
		title: tit,
		area: ['650px', '500px'],
		shade: 0,
		shadeClose: false,
		content: jQuery("#Img"),
		btn: ['确定'],
		btn1: function (index) {
			layer.close(index);
		}
	});


}

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
			keyWord: '',
			type: '',
			status: ''
        },
		showList: true,
		title: null,
		auth: {},
		dict: {}
	},
	methods: {
		clear_s: function () {
			$("#createTime").val('');
		},
		clear_e: function () {
			$("#endTime").val('');
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dict = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "审核";
            
            vm.getInfo(id)
		},
		download: function (event) {
			var createTime =   $("#createTime").val();
			var endTime =  $("#endTime").val();
			if (createTime && endTime && createTime.length > 0 && endTime.length > 0) {
				var startTmp = createTime.split("-");
				var endTmp = endTime.split("-");
				var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
				var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
				if (sd.getTime() > ed.getTime()) {
					alert("开始日期不能大于结束日期");
					return ;
				}
			}
			if (createTime && createTime.length > 0) {
				createTime += " 00:00:00";
			}
			if (endTime && endTime.length > 0) {
				endTime += " 23:59:59";
			}

			const url= "sys/record/exRecord?da="+new Date().getTime();
			var formData = `&record=flow&startTime=${createTime}&endTime=${endTime}&keyWord=${vm.q.keyWord}&type=${vm.q.type}&status=${vm.q.status}`
			window.location.href = baseURL + url + formData;
		},
		saveOrUpdate: function (event) {
			var url = vm.dict.id == null ? "sys/product/add" : "sys/product/edit";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dict),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/product/remove",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/product/"+id, function(r){
                vm.dict = r.data;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var createTime =   $("#createTime").val();
			var endTime =  $("#endTime").val();
			if (createTime && endTime && createTime.length > 0 && endTime.length > 0) {
				var startTmp = createTime.split("-");
				var endTmp = endTime.split("-");
				var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
				var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
				if (sd.getTime() > ed.getTime()) {
					alert("开始日期不能大于结束日期");
					return ;
				}
			}
			if (createTime && createTime.length > 0) {
				createTime += " 00:00:00";
			}
			if (endTime && endTime.length > 0) {
				endTime += " 23:59:59";
			}
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
					"startTime":createTime,
					"endTime":endTime,
					"keyWord": vm.q.keyWord,
					"type":vm.q.type,
					"status":vm.q.status
				},
                page:1
            }).trigger("reloadGrid");
		}
	}
});